package com.keffect.effects.animatedEffects;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;

import com.keffect.effects.AnimatedEffect;

/**
 * 
 * @author Matias Varela
 *         <p>
 *         Student of Licenciatura en Ciencias de la Computacion, at the
 *         Universidad Nacional del Sur (Argentina).
 *         <p>
 *         project site: http://code.google.com/p/keffect-project/
 * 
 */
public class FadeIn extends AnimatedEffect {

	// effect variables

	private static final int RULE = AlphaComposite.SRC_ATOP;
	private Composite[] comps;
	private int compsIndex = 0;
	private Composite comp;


	// *******************

	public FadeIn(long duration) {
		super(duration);
		this.setFrames((long) (60 * Math.ceil((float) duration / 1000)) / 2);

		comps = new Composite[(int) getFrames()];
		for (int i = 0; i < comps.length; i++) {
			float alpha = (float) Math.cos(2 * Math.PI * i / (getFrames() * 2));
			alpha += 1;
			alpha /= 2.0;
			comps[i] = AlphaComposite.getInstance(RULE, alpha);
		}
	}


	@Override
	public void initEffectProperties() {
		this.compsIndex = (this.comps.length) - 1;

	}


	@Override
	public boolean isRunning() {
		return compsIndex > 0;
	}


	@Override
	public void run() {
		compsIndex--;
		comp = comps[compsIndex];
	}


	@Override
	public void doPaintBefore(Graphics2D g) {
		// TODO Auto-generated method stub
		g.setComposite(comp);
	}


	@Override
	public void doPaintAfter(Graphics2D g) {
	}

}
